package com.itheima.test10;

/**
 * @Author:gy
 * @Date: 2025/09/01 14:33
 * @Description:
 */
public class MyRunable implements Runnable {
    //定义一个成员变量: 100张票
    private int ticketCount = 100;
    Object obj = new Object();
    @Override
    public void run() {
        /*卖票的逻辑  1.判断票大于0,才能卖票  并且知道是哪个窗口卖的
                    2.卖了一张票  ,票数需要减1
                    3.卖光之后,不能卖了
        * */
            while(true){
                boolean b = sellTicket();
                if (b){
                    break;
                }
            }

    }
      //锁对象是this
      public synchronized boolean sellTicket(){
          if(ticketCount <= 0){
              //不能卖了
              return true;
          }else{
              //可以卖票
              try {
                  Thread.sleep(100);
              } catch (InterruptedException e) {
                  throw new RuntimeException(e);
              }
              ticketCount--;
              System.out.println(Thread.currentThread().getName()+"正在卖票,还剩下"+ticketCount+"张票");
              return false;
          }
      }
}
